package com.nbpt;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.nbpt.db.AccountDBHelp;
import com.nbpt.db.UserDBhelp;

public class RegisterAcitvity extends Activity implements View.OnClickListener {
    private EditText mm1;
    private EditText mm2;
    private EditText zh;
    private EditText sj;
    private EditText nc;
    private Spinner xb;
    private Button btn;
    private String spinner;
    private int xb1;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        nc = findViewById(R.id.nc);
        xb = (Spinner)findViewById(R.id.xb);
        mm1 = findViewById(R.id.mima1);
        mm2 = findViewById(R.id.mima2);
        zh = findViewById(R.id.zhanghao);
        sj = findViewById(R.id.shouji);
        btn = findViewById(R.id.btn1);
        btn.setOnClickListener(this);

        xb.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                int curyid =i;
                TextView tv = (TextView)view;
                spinner=xb.getSelectedItem().toString();
                if (spinner.equals("男"))xb1=0;
                if (spinner.equals("女"))xb1=1;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }


    private final String sql_insert_table ="INSERT INTO tb_users (id,username,password,gender,phone,nc,xb)\n" +
            "VALUES ('1','admin','123','','152512231','bztwc','男');";

    @Override
    public void onClick(View view) {


        String nc1 = nc.getText().toString();
        String mima1 = mm1.getText().toString();
        String mima2 = mm2.getText().toString();
        String zhanghao = zh.getText().toString();
        String shouji = sj.getText().toString();



        Log.e("Main","手机号为"+shouji);
        Log.e("Main","用户名为"+zhanghao);
        Log.e("Main","第一次输入的密码为"+mima1);
        Log.d("Main","第二次输入的密码为"+mima2);
        Log.d("Main","昵称为"+nc1);



        AccountDBHelp helper = new AccountDBHelp(RegisterAcitvity.this,AccountDBHelp.VERSION_1);
        Cursor cursor = helper.select(null,null);
        cursor.moveToFirst();

        if (zhanghao.equals("")){
            Toast.makeText(this, "账号为空", Toast.LENGTH_LONG).show();
        }
        else if (mima1.equals(""))
        {
            Toast.makeText(this, "密码为空", Toast.LENGTH_LONG).show();
        }
        else if (!mima2.equals(mima1))
        {
            Toast.makeText(this, "两次密码不相同", Toast.LENGTH_LONG).show();
        }
        else if(shouji.equals(""))
        {
            Toast.makeText(this, "手机为空", Toast.LENGTH_LONG).show();
        }
        else if (!zhanghao.equals("")&&!mima1.equals("")&&!shouji.equals("")&&mima1.equals(mima2))
        {
            long run;
            run = helper.insert(zhanghao,mima1,shouji,nc1,xb1);
            if (run == -1)
            {
                Toast.makeText(this, "用户已被注册", Toast.LENGTH_LONG).show();
            }
            else
                {
                Toast.makeText(this, "注册成功", Toast.LENGTH_LONG).show();
                Intent data = new Intent();
                data.putExtra("username",zhanghao);
                data.putExtra("password",mima1);
                setResult(0,data);
                finish();
            }
        }

    }
}
